/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "HtmlEdit.hpp"
#include "HtmlEditSettings.hpp"
#include "SettingsDialog.hpp"
#include "Viewbase.hpp"



HtmlEdit::HtmlEdit( wxWindow* parent, int id /*= wxID_ANY*/ )
: wxStyledTextCtrl(parent, id, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE)
{
	// Read all options from registry
	ReadOptions();

	// Set Lexer to Html
	this->SetLexer(wxSTC_LEX_HTML);

	// Set Style Options
	SetMyStyle();
}

HtmlEdit::~HtmlEdit()
{
	// Save options before quitting
	WriteOptions();
}

void HtmlEdit::ReadOptions()
{
	wxConfig* cfg = new wxConfig;
	m_options.WordWrap = cfg->ReadBool("WordWrap", true);
	m_options.Font = cfg->Read("SourceFont", "");
	m_options.LineNumbers = cfg->ReadBool("LineNumbers", true);
	delete cfg;
}

void HtmlEdit::WriteOptions()
{
	wxConfig* cfg = new wxConfig;
	cfg->Write("WordWrap", m_options.WordWrap);
	cfg->Write("SourceFont", m_options.Font);
	cfg->Write("LineNumbers", m_options.LineNumbers);
	delete cfg;
}

void HtmlEdit::SetNewFont( wxFont& font )
{
	m_options.Font = font.GetNativeFontInfoDesc();
	SetMyStyle();
}

void HtmlEdit::SetMyStyle()
{
	// There are 3 Margins: Linenumbers, Folding and for Debugger Breakpoints etc.
	// We deactivate the other 2 always to get rid of the extra space.
	SetMarginType(1, 0);
	SetMarginWidth(1, 0);
	SetMarginType(2, 0);
	SetMarginWidth(2, 0);

	// Linenumbers
	if ( m_options.LineNumbers == true )
	{		
		SetMarginType(0, wxSTC_MARGIN_NUMBER);
		int mw = this->TextWidth(wxSTC_STYLE_LINENUMBER, "999");
		SetMarginWidth(0, mw);
		StyleSetForeground (wxSTC_STYLE_LINENUMBER, *wxBLACK);
		StyleSetBackground(wxSTC_STYLE_LINENUMBER, wxColour("#C0C0C0"));
	}
	else
	{
		SetMarginType(0, 0);
		SetMarginWidth(0, 0);
	}

	// Wrap Mode
	if ( m_options.WordWrap == true )
	{
		SetWrapMode(wxSTC_WRAP_WORD);
	}
	else
	{
		SetWrapMode(wxSTC_WRAP_NONE);
	}

	// General Font
	wxFont fnt;
	fnt.SetNativeFontInfo(m_options.Font);
	if (!fnt.IsOk())
	{
		fnt = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
		m_options.Font = fnt.GetNativeFontInfoDesc();
	}	
	StyleSetFont(wxSTC_STYLE_DEFAULT, fnt);	

	// Bold Style for Tags
	StyleSetBold(wxSTC_H_TAG, true);

	// Styles from Registry
	HtmlEditSettings s;
	const HtmlEditSettings::TStyleList styles = s.GetStyleList();
	HtmlEditSettings::TStyleList::const_iterator it = styles.begin();

	for ( it; it != styles.end(); ++it )
	{
		StyleDescriptor* d = it->second;
		StyleSetForeground(d->GetId(), d->GetForeground());
		StyleSetBackground(d->GetId(), d->GetBackground());
	}

	// All Red stuff is not wanted in a script
	StyleSetForeground(wxSTC_H_COMMENT, *wxRED); 
	StyleSetForeground(wxSTC_H_TAGUNKNOWN, *wxRED);
	StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, *wxRED);
	StyleSetForeground(wxSTC_H_DOUBLESTRING, *wxRED);

}

void HtmlEdit::SetWordWrap(bool state)
{
	m_options.WordWrap = state;
	SetMyStyle();
}

void HtmlEdit::ShowLineNumbers( bool state )
{
	m_options.LineNumbers = state;
	SetMyStyle();
}